package api.service;

import api.model.ApiColumn;
import m.common.model.Model;
import m.common.model.TableMeta;
import m.common.model.config.ModelConfig;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.service.Service;
import m.system.db.TransactionManager;
import m.system.exception.MException;
import m.system.util.ClassUtil;
import m.system.util.GenerateID;
import m.system.util.StringUtil;

public class ApiColumnService extends Service {

    public String save(ApiColumn model) throws Exception {
        String msg;
        TransactionManager tm=new TransactionManager();
        try {
            tm.begin(model.getOid());
            ModelCheckUtil.checkNotNull(model, "name","type","table.oid");
            if(!model.getName().matches("[a-z_0-9]*")) throw new MException(this.getClass(),"字段只能包含小写字母数字和下划线");
            ModelCheckUtil.checkUniqueCombine(model, new String[]{"name","table.oid"});
            if(model.getType().equals("STRING")&&null==model.getLength()) throw new MException(this.getClass(),"请输入长度");
            if(model.getType().equals("MODEL")&&StringUtil.isSpace(model.getModelClass())) throw new MException(this.getClass(),"请选择表");
            if(model.getType().equals("TABLE")&&StringUtil.isSpace(model.getModelTable().getOid())) throw new MException(this.getClass(),"请选择表");
            if(model.getType().equals("MODEL")||model.getType().equals("TABLE")){
                model.setLength(50);
                if(model.getType().equals("MODEL")){
                    TableMeta meta= ModelConfig.getTableMeta(ClassUtil.getClass(Model.class,model.getModelClass()));
                    model.setModelName(meta.name());
                    model.setModelDesc(meta.description());
                    model.setModelTable(null);
                }else{
                    model.setModelClass(null);
                    model.setModelName(null);
                    model.setModelDesc(null);
                }
            }else{
                if(!model.getType().equals("STRING")){
                    model.setLength(null);
                    model.setDictType("");
                }else{
                    String dict=StringUtil.noSpace(model.getDictType());
                    if(dict.contains(":")){
                        model.setDictOption(dict);
                        model.setDictType("");
                    }else{
                        model.setDictOption("");
                        model.setDictType(dict);
                    }
                }
                model.setModelClass(null);
                model.setModelName(null);
                model.setModelDesc(null);
                model.setModelTable(null);
            }
            model.setField(model.getName());
            if(StringUtil.isSpace(model.getOid())){
                model.setOid(GenerateID.generatePrimaryKey());
                model.setIsinit("N");
                ModelUpdateUtil.insertModel(model);
                msg="保存成功";
            }else{
                ModelUpdateUtil.updateModel(model, "name","field","description","type","length","dictType","dictOption","notnull","modelClass","modelName","modelDesc","modelTable.oid");
                msg="修改成功";
            }
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
        return msg;
    }
    public String delete(ApiColumn model) throws Exception {
        String msg;
        TransactionManager tm=new TransactionManager();
        try {
            tm.begin(model.getOid());
            model= ModelQueryList.getModel(model);
            //自建表已初始化不能删除
            if(model.getIsinit().equals("Y")) throw new MException(this.getClass(),"已初始化不能删除");
            ModelUpdateUtil.deleteModel(model);
            msg="删除成功";
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
        return msg;
    }
}
